#include "window_common.h"
#include "gkt_door.h"

#if defined(GKT_CONFIG_DOOR_LOCK_AUTOMATIC) && GKT_CONFIG_DOOR_LOCK_AUTOMATIC

enum {
	TIMER_ID_TIMEOUT = 0,
	TIMER_ID_MAX
};
typedef struct _window_motor_self_learn {
	uint32_t play_over;
} window_motor_self_learn_s;
static window_motor_self_learn_s s_window_motor_self_learn;

static void window_motor_self_learn_complete(window_result_e result)
{
	uint32_t voice_flags = GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR;

	gkt_window_timer_clear(TIMER_ID_TIMEOUT);

	if (WINDOW_RESULT_OPERATE_SUCCESS == result) {
		gp_window_unreset_data->lock_body_type = 
				gkt_door_lock_get_body_type();
		gp_window_unreset_data->lock_motor_torsion_gear
				= GKT_DOOR_LOCK_MOTOR_TORSION_GEAR_LOW;
		gp_window_unreset_data->lock_motor_full_rotate_time_ms = 
				gkt_door_lock_get_motor_rotate_time(1);
		gkt_info("window_motor_self_learn: body_type(%u) full_rotate_time(%u)\n",
			gp_window_unreset_data->lock_body_type,
			gp_window_unreset_data->lock_motor_full_rotate_time_ms);
#if (GKT_MODULE_LOC_FRONTBOARD == GKT_DOOR_LOCK_LOCATION)
		gkt_door_lock_get_motor_locked_rotor_voltage(
			gp_window_unreset_data->lock_motor_locked_rotor_voltage);
#endif
		gp_window_unreset_data->lock_motor_self_learn_complete = 1;
		window_data_save();
		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(operation_success), voice_flags, NULL);
	}
	else if (WINDOW_RESULT_OPERATE_FAILED == result)
		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(operation_failed), voice_flags, NULL);
	else
		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(operation_timeout), voice_flags, NULL);

	gkt_window_timeout_enable("window_motor_self_learn - complete");
	gkt_window_system_standby_enable();
	gkt_window_switch_control(1);
	s_window_motor_self_learn.play_over = 1;

}
static void window_motor_self_learn_please_wait(void)
{
	uint32_t voice_flags = GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR;
	gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(please_wait), voice_flags, NULL);
}
static int window_motor_self_learn_event_handler(
				uint32_t major_type, uint32_t minor_type, 
				uint32_t id, uint32_t data)
{
	uint32_t timeout;
	int retval = GKT_EVENT_HANDLE_CONTINUE;

	switch(major_type) {
		case GKT_EVENT_WINDOW:
			if (GKT_WINDOW_ACTIVE == minor_type) {
				s_window_motor_self_learn.play_over = 0;
				window_common_mask_all();

				gkt_window_switch_control(0);
				gkt_window_system_standby_disable();
				gkt_window_timeout_disable("window_motor_self_learn - start");

				retval = gkt_door_lock_motor_self_learn();
				if (retval != GKT_EINPROCESS && (retval != GKT_SUCCESS))
					window_motor_self_learn_complete(WINDOW_RESULT_OPERATE_FAILED);
				else {
					timeout = gkt_door_lock_get_timeout(GKT_DOOR_LOCK_OP_SELF_LEARN) + 2000;
					gkt_info("window_motor_self_learn: timeout(%u)\n", timeout);
					gkt_window_timer_set(TIMER_ID_TIMEOUT, timeout, 0, 0);
					window_motor_self_learn_please_wait();
				}
			}
			break;
		case GKT_EVENT_SYSTEM:
			if ((GKT_WINDOW_SYSTEM_DOOR == minor_type)
				&& (GKT_DOOR_NOTIFY_LOCK_STATUS == id)) 
			{
				gkt_info("window_motor_self_learn: status(%u)\n", data);
				if (GKT_DOOR_LOCK_ST_SELF_LEARN_COMPLETE == data)
					window_motor_self_learn_complete(WINDOW_RESULT_OPERATE_SUCCESS);
				else if (GKT_DOOR_LOCK_ST_SELF_LEARN_FAILED == data)
					window_motor_self_learn_complete(WINDOW_RESULT_OPERATE_FAILED);
			}
			retval = GKT_EVENT_HANDLE_COMPLETE;
			break;
		case GKT_EVENT_TIMER:
			if (TIMER_ID_TIMEOUT == minor_type)
				window_motor_self_learn_complete(WINDOW_RESULT_OPERATE_TIMEOUT);
		case GKT_EVENT_VOICE:
			if (s_window_motor_self_learn.play_over == 1) {
				s_window_motor_self_learn.play_over = 0;
				gkt_window_exit();
			}
			break;
		default:
			break;
	}

	return retval;
}
GKT_WINDOW_DESC_DEFINE_NO_WIDGETS(motor_self_learn);

#endif

